<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  @body_classes << 'padless-content'
  course_home ||= false
  js_env({
    :COLLAPSED_MODULES => @collapsed_modules,
    :CAN_MANAGE_MODULES => @can_edit,
    :IS_STUDENT => can_do(@context, @current_user, :participate_as_student),
    :COURSE_ID => @context.id,
    :NO_MODULE_PROGRESSIONS => @context.large_roster,
  })
  js_bundle :context_modules
  css_bundle :content_next
%>
<% if course_home %>
  <div class="screenreader-only"><%= @context.name %></div>
  <h2 class="context-modules-title screenreader-only">{{#t}}Course Modules{{/t}}</h2>
<% else %>
  <h1 class="context-modules-title screenreader-only">{{#t}}Course Modules{{/t}}</h1>
<% end %>

<% if @can_edit %>
  <div tabindex="0" class="drag_and_drop_warning screenreader-only" >
    <%= t('modules_keyboard_hint_updated',
          'Warning: For improved accessibility in reordering Modules (or Module items), please use the Move To Dialog option found in the menu.') %>
  </div>
<% end %>

<div class="header-bar">
  <div class="header-bar-right header-bar__module-layout">
    <div class="header-bar-right__buttons">
      <% if @can_edit %>
        <a class="btn module_progressions_link" href="<%= progressions_course_context_modules_path(@context) %>">{{#t}}View Progress{{/t}}</a>
      <% end %>
      <% if @allow_web_export_download %>
        <%= form_tag(context_url(@context, :context_start_offline_web_export_url)) do %>
          <button type="submit" class="btn offline_web_export">
            {{> icons/export}}
            {{#t}}Export Course Content{{/t}}
          </button>
        <% end %>
      <% end %>
      <% if @can_edit %>
        <button class="btn btn-primary add_module_link">
          {{> icons/plus}}
          <span class="screenreader-only">Add</span>
          {{#t}}Module{{/t}}
        </button>
      <% end %>
    </div>
    <% if @last_web_export %>
      <small class="muted">
        {{#t}}Last Export: {{/t}}
        <a href="<%= context_url(@context, :context_offline_web_exports_url) %>">
          <%= datetime_string(force_zone(@last_web_export.created_at)) %>
        </a>
      </small>
    <% end %>
  </div>
</div>

<div class="item-group-container" id="context_modules_sortable_container">
  <div id="no_context_modules_message" style="display:none;">
    <% if can_do(@context.context_modules.temp_record, @current_user, :create) %>
      <div id="modules_homepage_user_create"></div>
    <% else %>
      <p>{{#t}}No modules have been defined for this course.{{/t}}</p>
    <% end %>
  </div>
  <div
    id="context_modules"
    aria-label="{{#t}}Course Modules{{/t}}"
    class="ig-list <%= 'editable' if @can_edit %>"
  >
    <% cache(@modules_cache_key) do %>
      <% preload_modules_content(@modules, @can_edit) %>
      <% @modules.each do |m| %>
        {{> views/context_modules/context_module_next}}
      <% end %>
    <% end %>
  </div>

</div>

{{> views/context_modules/context_module_next}}
{{> views/context_modules/module_item_next}}

<% if @can_edit %>
  {{> views/context_modules/add_context_module_form}}

  <div id="add_module_prerequisite_dialog" style="display: none;">
    <span class="context_module_id" style="display: none;">&nbsp;</span>
    <div>
      <%= t 'add_prerequisite', "Add prerequisite to *%{module}*", :wrapper => '<span class="module_name">\1</span>', :module => 'module' %>
    </div>
    <div style="text-align: center;" class="prerequisite_module_select">
    </div>
    <div class="button-container">
      <button type="button" class="btn submit_button">{{#t}}Add Prerequisite{{/t}}</button>
      <button type="button" class="btn button-secondary cancel_button">{{#t}}Cancel{{/t}}</button>
    </div>
  </div>

  {{> views/shared/select_content_dialog}}
  <div id="edit_item_dialog" style="display: none;">
    <%= form_for :content_tag, :url => "", :html => {:id => "edit_item_form", :method => :put, :style => "margin: 20px 10px;"} do |f| %>
      <p tabindex="0"> </p>
      <table class="formtable">
        <tr>
          <td><%= f.blabel :title, :en => "Title" %></td>
          <td><%= f.text_field :title, :style => "width: 200px;" %></td>
        </tr>
        <tr class="external external_url">
          <td><%= f.blabel :url, :en => "URL" %></td>
          <td><%= f.text_field :url, :style => "width: 200px;" %></td>
        </tr>
        <tr>
          <td><label for="content_tag_indent_select"><%= before_label('indent', %{Indent}) %></label></td>
          <td>
            <label class="screenreader-only" for="content_tag_indent_select">{{#t}}Select indentation{{/t}}</label>
            <select name="content_tag[indent]" id="content_tag_indent_select">
              <% 0.upto(3) do |i| %>
                <option value="<%= i %>"><%= t('indent.n', { :zero => %{Don't Indent}, :one => %{Indent 1 Level}, :other => %{Indent %{count} Levels} }, :count => i) %></option>
              <% end %>
            </select>
          </td>
        </tr>
        <tr class="external">
          <td colspan="2">
            <%= f.check_box :new_tab %>
            <%= f.label :new_tab, :en => "Load in a new tab" %>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <div class="button-container">
              <button type="button" class="btn cancel_button">{{#t}}Cancel{{/t}}</button>
              <button type="submit" class="btn btn-primary">{{#t}}Update{{/t}}</button>
            </div>
          </td>
        </tr>
      </table>
    <% end %>
    <form>
    <table>
    </table>
    </form>
  </div>
  <div id="add_prerequisite_dialog" style="display: none;">
    <h3><%= t 'headings.add_prerequisite', 'Add Prerequisite to %{name}', :name => '<span class="name">&nbsp;</span>'.html_safe %></h3>
    <p style="display: none;">{{#t}}Select the type of prerequisite.  Students cannot see a module until all prerequisites are satisfied.{{/t}}
      <div>
        <label class="screenreader-only" for="prerequisite_type_select">{{#t}}Select type of prerequisite{{/t}}</label>
        <select id="prerequisite_type_select" class="type" style="display: none;">
          <option value="module">{{#t}}Another Module{{/t}}</option>
        </select>
      </div>
    </p>
    <p class="module_details"><%= t 'module_details', %{Modules are shown in sequential order, so only modules that are above %{name} can be set as prerequisites.}, :name => '<span class="name">&nbsp;</span>'.html_safe %>
      <div>
        <label class="screenreader-only" for="prerequisite_module_select">{{#t}}Select prerequite module{{/t}}</label>
        <select id="prerequisite_module_select" class="module_id">
        </select>
      </div>
    </p>
  </div>
<% end %>
<div style="display: none;">
  <a href="<%= context_url(@context, :progressions_context_context_modules_url) %>" class="progression_list_url">&nbsp;</a>
  <a href="<%= context_url(@context, :context_context_modules_assignment_info_url) %>" class="assignment_info_url">&nbsp;</a>
  <select id="module_list" aria-label="{{#t}}Select prerequisite module{{/t}}">
    <option value="">[ {{#t}}Select Module{{/t}} ]</option>
    <% @modules.each do |m| %>
      <option value="<%= m.id %>" class="context_module_option context_module_<%= m.id %>"><%= t 'select_module.name', '%{module}', :module => m.name %></option>
    <% end %>
  </select>
  <div id="display_criterion_blank" class="criterion" style="display: none; float: left;">
    <span class="id" style="display: none;">&nbsp;</span>
    <span class="type" style="display: none;">&nbsp;</span>
    <span class="name">{{#t}}module{{/t}}</span>
    <span><a href="#" class="delete_prerequisite_link" title="{{#t}}Remove this prerequisite{{/t}}">&#215;</a></span>
  </div>
  <div id="completion_criterion_option" class="completion_criterion_option">
    <% ot 'criterion_for_assignment', '%{assignment} %{select_criterion}', :assignment => capture { %>
    <span style="padding: 0 4px;">
      <select class="id assignment_picker" tabindex="0" aria-label="{{#t}}Requirement subject{{/t}}"></select>
    </span>
    <% }, :select_criterion => capture { %>
    <span style="padding: 0 4px;">
      <select class="type assignment_requirement_picker" tabindex="0" aria-label="{{#t}}Requirement{{/t}}">
        <option class="any" value="must_view">{{#t}}view the item{{/t}}</option>
        <option class="assignment wiki_page" value="must_mark_done"><%= t(%{mark as done}) %></option>
        <option class="wiki_page discussion_topic" value="must_contribute">{{#t}}contribute to the page{{/t}}</option>
        <option class="assignment quiz graded" value="must_submit">{{#t}}submit the assignment{{/t}}</option>
        <option class="assignment quiz graded" value="min_score">{{#t}}score at least{{/t}}</option>
      </select>
    </span>
    <% } %>
    <span class="min_score_box" style="display: none;" tabindex="0">
      <label class="screenreader-only" for="criterion_min_score">{{#t}}Minimum Score{{/t}}</label>
      <input id="criterion_min_score" type="text" class="min_score" style="width: 30px; margin-right: 5px;"/>
      <span class="points_possible_parent"><%= t 'assignment_points_possible', '/ %{points_possible}', :points_possible => '<span class="points_possible">&nbsp;</span>'.html_safe %></span>
    </pan>
  </div>
  <%= image_tag "flagged_question_dim.png", :id => "criterion_image_min_score", :class => "image not_blank", :alt => t('images.alt.min_score', "minimum score"), :title => t('images.title.min_score', "Students must get at least a %{min_score} on this item before the module is considered complete", :min_score => "{{ min_score }}") %>
  <%= image_tag "preview_dim.png", :id => "criterion_image_must_view", :class => "image not_blank", :alt => t('images.alt.must_view', "must view"), :title => t('images.title.must_view', "Students must view this item before the module is considered complete") %>
  <%= image_tag "text_entry_dim.png", :id => "criterion_image_must_submit", :class => "image not_blank", :alt => t('images.alt.must_submit', "must submit"), :title => t('images.title.must_submit', "Students must submit this assignment before the module is considered complete") %>
  <%= image_tag "text_entry_dim.png", :id => "criterion_image_must_contribute", :class => "image not_blank", :alt => t('images.alt.must_contribute', "must contribute"), :title => t('images.title.must_contribute', "Students must contribute to this item before the module is considered complete") %>
  <%= image_tag "blank.png", :id => "criterion_image_blank", :class => "image", :alt => "" %>
</div>
<div id="current_user_progression_list" class="user_<%= @current_user && @current_user.id %>" style="display: none;">
  <div class="progression progression_blank">
    <div class="context_module_id">&nbsp;</div>
    <div class="workflow_state">&nbsp;</div>
    <div class="requirements_met">&nbsp;</div>
    <div class="current_position">&nbsp;</div>
    <div class="collapsed">&nbsp;</div>
  </div>
</div>

{{> views/context_modules/keyboard_navigation}}
